package top.ljlgithub.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

/**
 * @ClassName
 * @Description TODO
 * @Author L1470
 * @Date 2021/3/21 8:08
 * @Version 1.0
 **/
public class MybatisUtil {
    private static SqlSessionFactory factory = null;
    static {
        try {
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            InputStream is = Resources.getResourceAsStream("conf/mybatis.xml");
            factory = builder.build(is);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static SqlSession getSqlSession(){
        if (factory != null){
            return factory.openSession();
        }
        return null;
    }
}
